#coding:utf-8
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
import time

class UserLog( models.Model):
    """
    自定义日志
    """
    FINISHED = 0
    RUNNING = 1
    FAIL = 2
    STATU_CHOICES = (
        (FINISHED, '完成'),
        (RUNNING, '操作中'),
        (FAIL, '失败'),
    )
    #name = models.CharField('操作名', help_text='操作名，不唯一，有菜单记录的为菜单名，或者操作命令名', max_length=100, default='内部处理')
    method = models.CharField('访问方式', max_length=300)
    REMOTE_ADDR=models.CharField('访问IP', max_length=300)
    HTTP_USER_AGENT=models.CharField('User-Agent', max_length=300)
    PATH_INFO=models.CharField('访问路径', max_length=300)
    user = models.CharField(verbose_name='用户',max_length=300)
    namespace = models.CharField(help_text='app_label同名, dajaxice没有这个字段', max_length=300, blank=True)
    url_name = models.CharField('view名', max_length=300, default='', help_text='任一链接由namespace和url_name决定')
    get_args = models.TextField('get数据', help_text='get的参数数据', blank=True)
    post_args = models.TextField('post数据', help_text='post的参数数据', blank=True)
    file_args = models.TextField('file数据', help_text='可能只是一个名字', blank=True)
    start_datetime = models.DateTimeField('开始执行时间', auto_now_add=True)
    end_datetime = models.DateTimeField('完成/失败时间', auto_now_add=True)
    spend_time = models.IntegerField('消耗时间', help_text='单位为秒', editable=False, blank=True, null=True)
    status = models.IntegerField('执行状态', choices=STATU_CHOICES, help_text='判断一个访问是否成功')

    class Meta:
        verbose_name = "操作日志"
        verbose_name_plural = verbose_name

    # def save(self, *args, **kwargs):
    #     if self.status == 0:
    #         self.spend_time = int(time.time()) - time.mktime(self.start_datetime.timetuple())
    #     return super(UserLog, self).save(*args, **kwargs)

    class Meta:
        ordering = ['-id']
